"""
根据逆波兰表示法，求表达式的值。

有效的运算符包括 +, -, *, / 。每个运算对象可以是整数，也可以是另一个逆波兰表达式。

说明：

    整数除法只保留整数部分。
    给定逆波兰表达式总是有效的。换句话说，表达式总会得出有效数值且不存在除数为 0 的情况。

输入: ["2", "1", "+", "3", "*"]
输出: 9
解释: ((2 + 1) * 3) = 9

注意除法小于0的情况
"""


class Solution:
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        stack = []
        compute = ["+","-","*","/"]
        for x in tokens:
            if x not in compute:
                stack.append(int(x))
            else:
                a = stack.pop()
                b = stack.pop()
                s = 0
                if x == "+":
                    s = a+b
                elif x == "-":
                    s = b-a
                elif x == "*":
                    s = a*b
                elif x == "/":
                    if a*b < 0:
                        s = -((-b)//a)
                    else:
                        s = b//a
                stack.append(s)
        return stack.pop()


s = Solution()
print(s.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))